using System.Collections.Generic;
using System.IO;
using System.Text;
using pal.SvnConf.Core;

namespace pal.SvnConf.Tests.Core
{
    internal class TestUtil
    {
        private PasswdFile _samplePasswdFile;
        private AuthzFile _sampleAuthzFile;


        public TestUtil()
        {
            _samplePasswdFile = new PasswdFile(new StringReader(SamplePasswdFile));
            _sampleAuthzFile = new AuthzFile(new StringReader(SampleAuthzFile), _samplePasswdFile);
        }

        public static string SampleAuthzFile
        {
            get
            {
                return new StringBuilder()
                    .AppendLine("[groups]")
                    .AppendLine("group1 = user11,user12")
                    .AppendLine("group2 = user21,user22,user23")
                    .AppendLine("group3 = user11,user23")
                    .AppendLine("group4 = @group1,user23")
                    .AppendLine()
                    .AppendLine("[/foo/bar]")
                    .AppendLine("user12 = r")
                    .AppendLine("user11 = rw")
                    .AppendLine("@group3 = r")
                    .AppendLine("[repository1:/whatever/path]")
                    .AppendLine("@group4 = rw")
                    .AppendLine("* = r")
                    .ToString();
            }
        }

        public static string SamplePasswdFile
        {
            get
            {
                return new StringBuilder()
                    .AppendLine("[users]")
                    .AppendLine("user11 = password11")
                    .AppendLine("user12 = password12")
                    .AppendLine("user21 = password21")
                    .AppendLine("user22 = password22")
                    .AppendLine("user23 = password23")
                    .ToString();
            }
        }

        public PasswdFile PasswdFile
        {
            get { return _samplePasswdFile; }
        }

        public AuthzFile AuthzFile
        {
            get { return _sampleAuthzFile; }
        }

        public static Acl FooBarAcl
        {
            get
            {
                List<AuthorizationEntry> authorizationEntries =
                    new List<AuthorizationEntry>();
                authorizationEntries.Add(new AuthorizationEntry("user12", "r"));
                authorizationEntries.Add(new AuthorizationEntry("user11", "rw"));
                authorizationEntries.Add(new AuthorizationEntry("@group3", "r"));
                return new Acl("/foo/bar", authorizationEntries);
            }
        }

        public TestUtil ParseAll()
        {
            _samplePasswdFile.Parse();
            _sampleAuthzFile.Parse();
            return this;
        }
    }
}